package com.bocai.grainshop.order.web;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.bocai.grainshop.order.service.OrderService;
import com.bocai.grainshop.order.vo.PayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
public class PayController {

    @Autowired
    private OrderService orderService;


    /**
     * @Author: SunJinbo
     * @Description  支付宝支付
     * @Param [orderSn]
     * @return java.lang.String
     * @Data Created in 2022/5/4 20:04
     */
    @ResponseBody
    @GetMapping(value = "/aliPayOrder",produces = "text/html")
    public String aliPayOrder(@RequestParam("orderSn") String orderSn) throws Exception {

        PayVo payVo = orderService.getOrderPay(orderSn);

        AlipayTradePagePayResponse payResponse = Factory.Payment.Page().pay(payVo.getSubject(), payVo.getOut_trade_no(),
                payVo.getTotal_amount(), payVo.getRetrunUrl());
        if (ResponseChecker.success(payResponse)) {
            log.info("{支付宝响应——》}");
            return payResponse.getBody();
        }
        return "";
    }

}
